//
//  WebViewController.m
//  Car
//
//  Created by apple on 15/9/19.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "WebViewController.h"
#import "RecommendModel.h"
#import "ProgressHUD.h"

#import "RecemmendTableViewController.h"

@interface WebViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, strong) NSString *htmlUrl;


@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	[ProgressHUD show:@"加载中"];
    [self reloadWithJson];
    
	
    
//    [self leftButtonItem];
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [ProgressHUD dismiss];
}


- (void)leftButtonItem {
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(actionClick)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
}


- (void)actionClick {
    
    [ProgressHUD dismiss];
        
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}


- (void)reloadWithJson {
    
//  NSString *aaa = @"http://cont.app.autohome.com.cn/autov4.8.8/content/news/newsinfo-pm1-i%@.json";
    
    NSString *url = [NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.8.8/content/news/newsinfo-pm1-i%@.json", self.changedID];
    
    [LORequestManger GET:url success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *ric = dic[@"result"];
        RecommendModel *model = [RecommendModel shareJsonWithDictionary:ric];
        
        self.htmlUrl = model.url;
        
        if (self.htmlUrl != nil) {
            self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
            self.webView.delegate = self;
            
            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.htmlUrl]];
            
            [self.webView loadRequest:urlRequest];
            [self.view addSubview:self.webView];

        }
        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"二级页面加载失败");
        
    }];
    
    
    
    
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
	
	//删除顶部导航
	NSMutableString *js1 = [NSMutableString string];
	[js1 appendString:@"var post = document.getElementsByClassName('nav-bread')[0];"];
	[js1 appendString:@"post.parentNode.removeChild(post);"];
	
	[webView stringByEvaluatingJavaScriptFromString:js1];
	//删除广告星星
	NSMutableString *js3 = [NSMutableString string];
	[js3 appendString:@"var ad = document.getElementsByClassName('iconfont icon-star')[0];"];
	[js3 appendString:@"ad.parentNode.removeChild(ad);"];
	[webView stringByEvaluatingJavaScriptFromString:js3];
 /**
  *comment
//  */

		NSMutableString *js2 = [NSMutableString string];
		[js2 appendString:@"var handle = document.getElementsByClassName('comment-publish fn-mlr disabled')[0];"];
		[js2 appendString:@"handle.parentNode.removeChild(handle);"];
		
		[webView stringByEvaluatingJavaScriptFromString:js2];
 /**
  *  list
  */
	NSMutableString *js4 = [NSMutableString string];
	[js4 appendString:@"var list = document.getElementsByClassName('list-comment')[0];"];
	[js4 appendString:@"list.parentNode.removeChild(list);"];
	
	[webView stringByEvaluatingJavaScriptFromString:js4];
 /**
  *  tab
  */
	NSMutableString *js5 = [NSMutableString string];
	[js5 appendString:@"var tab = document.getElementsByClassName('tab')[0];"];
	[js5 appendString:@"tab.parentNode.removeChild(tab);"];
	
	[webView stringByEvaluatingJavaScriptFromString:js5];
	/**
	 *  adv03 fn-mt fn-mlr
	 */
	NSMutableString *js6 = [NSMutableString string];
	[js6 appendString:@"var ad1 = document.getElementsByClassName('adv03 fn-mt fn-mlr')[0];"];
	[js6 appendString:@"ad1.parentNode.removeChild(ad1);"];
	
	[webView stringByEvaluatingJavaScriptFromString:js6];
	
	/**
	 *  application fn-mt fn-mb fn-mlr
	 */
	
	NSMutableString *js7 = [NSMutableString string];
	[js7 appendString:@"var ad2 = document.getElementsByClassName('application fn-mt fn-mb fn-mlr')[0];"];
	[js7 appendString:@"ad2.parentNode.removeChild(ad2);"];
	
	[webView stringByEvaluatingJavaScriptFromString:js7];
	/**
	 *  news-series fn-mt
	 */
	NSMutableString *js8 = [NSMutableString string];
	[js8 appendString:@"var ad3 = document.getElementsByClassName('news-series fn-mt')[0];"];
	[js8 appendString:@"ad3.parentNode.removeChild(ad3);"];
	
	[webView stringByEvaluatingJavaScriptFromString:js8];
	/**
	 *  adv03 fn-mt fn-mlralert(ad4);
	 */
	NSMutableString *js9 = [NSMutableString string];
	[js9 appendString:@"var ad4 = document.getElementById('Reply').innerHTML;"];
	[js9 appendString:@"ad4.parentNode.removeChild(ad4);"];
	
	[webView stringByEvaluatingJavaScriptFromString:js9];
	

    [ProgressHUD dismiss];

}
- (void)viewWillDisappear:(BOOL)animated{
    [ProgressHUD dismiss];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
